package edu.xlh.travel.service.impl;

import edu.xlh.travel.common.GalleryInRedisConstant;
import edu.xlh.travel.common.Result;
import edu.xlh.travel.dao.GallleryDao;
import edu.xlh.travel.enumeration.SFTPOperateTypeEnum;
import edu.xlh.travel.pojo.Gallery;
import edu.xlh.travel.service.GalleryService;
import edu.xlh.travel.util.FTPUtil;
import edu.xlh.travel.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@Service
@Transactional
public class GallleryServiceImpl implements GalleryService {
    @Autowired
    private GallleryDao gallleryDao;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private IdWorker idWorker;
    @Value("http://${ftp-centos.host}")
    private String ftpCentosIp;


    @Override
    public String findAllGallery(Model model) {
        List<Gallery> galleryList = gallleryDao.findAll();
        model.addAttribute("gallerys",galleryList);
        return "page/gallery";
    }

    @Override
    public Result findAll() {
        List<Gallery> galleryList = gallleryDao.findAll();
        if (galleryList!=null && galleryList.size()>0) {
            return new Result(200 , "返回景点图片列表成功" , galleryList);
        }
        return new Result(201 , "返回景点列表失败");
    }

    @Override
    public Result uploadFile(MultipartFile file) throws Exception {
            try {
                //获取文件名
                String filename = file.getOriginalFilename();
                //log.info(filename);
                //起别名
                int index = filename.lastIndexOf(".");
                String suffix = filename.substring(index);
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                String dateNowStr = sdf.format(date);
                String prefix  = UUID.randomUUID().toString().replace("-", "");
                filename=dateNowStr+"_"+prefix+"_gallery"+getNextImgNo()+suffix;
                //开始上传
                FTPUtil.sshSftp(file.getBytes(), filename , SFTPOperateTypeEnum.put.getCode());
                redisTemplate.boundSetOps(GalleryInRedisConstant.Gallery_RD_RESOURCES).add(filename);

                //log.info("最终文件的访问路径："+uploadVO.getFileName());
                    return new Result(200 , "相册名返回成功",filename);
            } catch (IOException e) {
                //log.error("图片上传失败"+upload.getOriginalFilename());
                //throw new MyException(ResultCodeEnum.SERVICE_ERROR);
                System.out.println(e.getMessage());
                return new Result(202);
            }
    }

    @Override
    public Result addGallery(Gallery gallery) {
        gallery.setId(idWorker.nextId()+"");
        String image = gallery.getImg();
        gallery.setImg(ftpCentosIp+"/"+image);
        Gallery saveGallery = gallleryDao.save(gallery);
        if (saveGallery!=null) {
            redisTemplate.boundSetOps(GalleryInRedisConstant.Gallery_DB_RESOURCES).add(image);
            return new Result(200 , "添加成功");
        }
            return new Result(201 , "添加失败");
    }

    public int getNextImgNo() {
        List<Gallery> galleryList = gallleryDao.findAll(Sort.by(Sort.Direction.DESC, "id"));
        int to = galleryList.get(0).getImg().lastIndexOf(".");
        int from = galleryList.get(0).getImg().lastIndexOf("gallery")+7;
        return Integer.parseInt(galleryList.get(0).getImg().substring(from, to))+1;
    }
}






















































































